package com.sjg.sys.utils;

import com.alibaba.fastjson.JSONArray;
import com.sjg.sys.entity.SysListRow;
import com.sjg.sys.entity.SysReturnInfo;
import com.sjg.sys.entity.SysUser;
import com.sun.org.apache.xpath.internal.SourceTree;
import org.apache.commons.lang.StringUtils;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.lang.reflect.Field;
import java.math.BigDecimal;
import java.text.DateFormat;
import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * 公共
 * 
 * @author tanghom <tanghom@qq.com> 2015-11-18
 */
public class CommUtil {
	
	
	/**
	 * 获取session
	 * @return
	 */
	public static HttpSession getSession(){
		HttpServletRequest request = ((ServletRequestAttributes)RequestContextHolder.getRequestAttributes()).getRequest();
		return request.getSession();
	}

	/**
	 * 获取当前登陆用户
	 * @return
	 */
	public static SysUser getCurUser(){
		return (SysUser) CommUtil.getSession().getAttribute(SysConstant.SESSION_ATTR_USER);
	}

	/**
	 * 判断是否POST提交，是POST返回true，不是POST提交返回false
	 * 
	 * @return 是POST返回true，不是POST提交返回false
	 */
	public static Boolean isPost() {
		HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
		if ("POST".equals(request.getMethod())) {
			return true;
		} else {
			return false;
		}
	}
	/**
	 *
	 * ajax反馈信息
	 * @param state 状态
	 * @param info 信息
	 * @param data 数据
	 * @return
	 */
	public static SysReturnInfo rtnmsg(Boolean state, String info, Object data) {
		SysReturnInfo returnInfo=new SysReturnInfo();
		returnInfo.setSuccess(state);
		returnInfo.setInfo(info);
		returnInfo.setData(data);
		return returnInfo;
	}

	/**
	 * ajax反馈信息
	 * @param state 状态
	 * @param info 信息
	 * @return
	 */
	public static SysReturnInfo rtnmsg(Boolean state, String info) {
		SysReturnInfo returnInfo=new SysReturnInfo();
		returnInfo.setSuccess(state);
		returnInfo.setInfo(info);
		return returnInfo;
	}

	/**
	 * 获取系统主键
	 * @return
	 */
	public static Long getPrimaryKey(){
		SysUUID sysUUID=SysUUID.getIstance();
		return sysUUID.generate();
	}

	public static <T> List<SysListRow> getSysListRows(List<T> list,String namefield,String valuefield){
		List<SysListRow> listRows=new ArrayList<>();
		if(list==null||list.get(0)==null){
			return listRows;
		}
		// 得到类对象
		Class userCla = (Class) list.get(0).getClass();
		Field[] fs = userCla.getDeclaredFields();//子类属性
		Field[] fsf=userCla.getSuperclass().getDeclaredFields();//父类属性
		Field[] fsall=Arrays.copyOf(fs,fs.length+fsf.length);
		System.arraycopy(fsf,0,fsall,fs.length,fsf.length);
		for (T obj:list) {
			SysListRow listRow=new SysListRow();
			String name= null;
			String value= null;
			for (int i = 0; i < fsall.length; i++) {
				Field f = fsall[i];
				f.setAccessible(true); // 设置些属性是可以访问的
				try {
					if(namefield.equals(f.getName())){
						name = String.valueOf(f.get(obj));
					} else if(valuefield.equals(f.getName())){
						value = String.valueOf(f.get(obj));
					}
				} catch (IllegalAccessException e) {
					e.printStackTrace();
				}

			}
			if(StringUtils.isBlank(name)||StringUtils.isBlank(value)){
				continue;
			}
			listRow.setName(name);
			listRow.setValue(value);
			listRows.add(listRow);
		}
		return listRows;

	}

	
}
